home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_097 / splines / cmds.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  245 lines

  1. /* The routines in this file are copyright (c) 1987 by Helene (Lee) Taran.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  */
  5.  
  6. #include "spline.h"
  7.  
  8. extern DLIST_ELEMENT Control_Points;
  9. extern struct PopUp_Menu PointMenu, CurveMenu;
  10. extern struct Image control_image;
  11.  
  12. int DrawAFrame = TRUE;
  13. int CurveType = OPENB_NATURAL;
  14.  
  15.  
  16. /* InRange : returns TRUE iff the mouse position is in the selection
  17.  * range of the given control point position
  18.  */
  19. int InRange(mouse, control)
  20. REAL_POINT *mouse, *control;
  21. {
  22.    return ((mouse->x >= control->x - CONTROL_RADIUS) &&
  23.            (mouse->x <= control->x + CONTROL_RADIUS) &&
  24.            (mouse->y >= control->y - CONTROL_RADIUS) &&
  25.            (mouse->y <= control->y + CONTROL_RADIUS));
  26. }
  27.  
  28. /* Select_ControlPoint : returns the member of Control_Points that is
  29.  * is selected at <x,y>. Returns FALSE if no point is selected.
  30.  */
  31. DLISTPTR Select_ControlPoint(x,y)
  32. SHORT x,y;
  33. {
  34.    REAL_POINT tmp; int InRange();
  35.    DLISTPTR Find_Element();
  36.  
  37.    tmp.x = (float) x;
  38.    tmp.y = (float) y;
  39.    return(Find_Element(&tmp,&Control_Points,InRange));
  40. }
  41.  
  42.  
  43. /* Print Instruction: prints a string in the upper left hand corner
  44.  * of the window.  Assumes that the current pen color is the the
  45.  * color you want the string to appear in.
  46.  */
  47. PrintInstruction(w,instr)
  48. struct Window *w;
  49. char *instr;
  50. {
  51.    Move(w->RPort,w->LeftEdge + w->BorderLeft + 1, 
  52.                  w->TopEdge  + w->BorderTop + 1);
  53.    Text(w->RPort,instr,strlen(instr));
  54. }
  55.  
  56. /* GetPoint : Waits for the user to select a point in window <w> and returns its
  57.  * x,y coordinates in terms of a REAL_POINT structure.
  58.  */
  59. DLISTPTR GetPoint(w) 
  60. struct Window *w;
  61. {
  62.    REAL_POINT *tmp; void *calloc(); DLISTPTR p;
  63.    struct IntuiMessage mcopy, *msg, *GetMsg();
  64.    struct RastPort *rp = w->RPort;
  65.    BYTE old_mode = rp->DrawMode;
  66.  
  67.    if (!((tmp = (REAL_POINT *)calloc(1,sizeof(REAL_POINT))) &&
  68.          (p = (DLISTPTR)calloc(1,sizeof(DLIST_ELEMENT))))) {
  69.       fprintf(stderr,"splines : trouble in paradise : can't allocate point\n");
  70.       panic(0);
  71.    }
  72.  SetDrMd(rp,COMPLEMENT);
  73.  PrintInstruction(w,"Select A Point");
  74.  while (1) {
  75.    Wait(1 << w->UserPort->mp_SigBit);
  76.    while (msg = GetMsg(w->UserPort)) {
  77.          mcopy = *msg;
  78.          ReplyMsg(msg);
  79.          if ((msg->Class == MOUSEBUTTONS) && (msg->Code == SELECTDOWN) &&
  80.              Inside_Window(msg->MouseX,msg->MouseY,w)) {
  81.             tmp->x = (float)msg->MouseX;
  82.             tmp->y = (float)msg->MouseY;
  83.             PrintInstruction(w,"Select A Point");
  84.             SetDrMd(rp,old_mode);
  85.             PlopDot(w,tmp);
  86.             p->contents = tmp;
  87.             return(p); 
  88.          }
  89.     }
  90.   }
  91. }
  92.  
  93.  
  94. /* PrintError: prints a error string to the window's upper left hand
  95.  * corner for approx. 1.6 seconds.  The string is drawn in the complement
  96.  * of the background so that it doesn't destroy what's already drawn in the
  97.  * window. After the delay, the string is complemented out again so it
  98.  * disappears.
  99.  */
  100. PrintError(w,str)
  101. struct Window *w;
  102. char *str;
  103. {   struct RastPort *rp = w->RPort;
  104.     BYTE old_mode = rp->DrawMode;
  105.     SetDrMd(rp,COMPLEMENT);
  106.     PrintInstruction(w,str);
  107.     Delay(80l);
  108.     PrintInstruction(w,str);
  109.     SetDrMd(rp,old_mode);
  110. }
  111.  
  112.  
  113. DrawConstructionLine(w,p0,p1) /* draws a dotted line from p0 to p1 */
  114. struct Window *w;
  115. REAL_POINT *p0,*p1;
  116. {   struct RastPort *rp = w->RPort;
  117.     BYTE old_pen = rp->FgPen;         /* save the old pen color */
  118.     USHORT old_pat = rp->LinePtrn;    /* save the old line pattern */
  119.  
  120.     SetAPen(rp,AFRAME_COLOR);
  121.     SetDrPt(rp,0xcccc);               /* use a dotted line */
  122.     Move(rp,(SHORT)p0->x,(SHORT)p0->y);
  123.     Draw(rp,(SHORT)p1->x,(SHORT)p1->y);
  124.     SetDrPt(rp,old_pat);
  125.     SetAPen(rp,old_pen);
  126.  
  127. }
  128.  
  129. /* Edit_Curve : pops up the curve style editing menu in <w> and allows
  130.  * the user to alter the current curve style
  131.  */
  132. Edit_CurveStyle(w)
  133. struct Window *w;
  134. {
  135.   int option = PopUp(&CurveMenu,w);
  136.   switch (option) {
  137.     case TOGGLEAFRAME : DrawAFrame = !DrawAFrame; Redraw(w); break;
  138.     case REDRAW : Redraw(w); break;
  139.     case OPENB_TRIPLE: 
  140.       if (LENGTH(&Control_Points) < 4) {
  141.         PrintError(w,"?Error: Triple Knot option require minimum of 4 points");
  142.         return; }
  143.         /* othewise fall through to action for the next case */
  144.     case OPENB_NATURAL : case CLOSEDB : case CLOSED_INTRPL : case OPEN_INTRPL :
  145.       if (option != CurveType) { CurveType = option; Redraw(w);}
  146.       break;
  147.     case QUIT : close_things(); exit(0); break;
  148.     }
  149. }
  150.  
  151.    
  152. /* Edit_ControlPoint : pops up the control point editing menu in <w> and
  153.  * allows the user to alter the given control point <p>.  Assumes
  154.  * that <p> is the control point that the user just selected.
  155.  */
  156. Edit_ControlPoint(w,p) 
  157. struct Window *w;
  158. DLISTPTR p;
  159. {  
  160.    int option = PopUp(&PointMenu,w);
  161.    switch (option) {
  162.      case ADD_AFTER    : 
  163.         if (LENGTH(&Control_Points) == MAXG) {
  164.            PrintError(w,"No! Don't you think you've added enough points?");
  165.            return;
  166.         }
  167.        else Insert_After(p,GetPoint(w),&Control_Points); break;
  168.  
  169.      case ADD_BEFORE   :
  170.         if (LENGTH(&Control_Points) == MAXG) {
  171.            PrintError(w,"No! Don't you think you've added enough points?");
  172.            return;
  173.         }
  174.        else Insert_Before(p,GetPoint(w),&Control_Points); break;
  175.  
  176.      case MOVE_POINT   : 
  177.         { DLISTPTR tmp;
  178.           EraseDot(w,p->contents); free(p->contents);
  179.           tmp = GetPoint(w); p->contents = tmp->contents;
  180.       free(tmp); break;
  181.         }
  182.      case REMOVE_POINT : 
  183.      { int len = LENGTH(&Control_Points);
  184.        if ((len == 4) && (CurveType == OPENB_TRIPLE)) {
  185.           PrintError(w,"?Error: Triple Knot option require minimum of 4 points");
  186.           return; }
  187.        else if (len == 3) 
  188.           PrintError(w,"?Error: you must have a minimum of 3 control points");
  189.        else {
  190.              Remove_Element(p,&Control_Points); 
  191.              free(p->contents); free(p);
  192.            }
  193.        } break;
  194.      default : return;
  195.    }
  196.    Redraw(w);
  197. }
  198.  
  199.  
  200. EraseDot(w,dot)
  201. struct Window *w;
  202. REAL_POINT *dot;
  203.     control_image.PlanePick = 0;
  204.     PlopDot(w,dot);
  205.     control_image.PlanePick = 1;
  206. }
  207.  
  208.  
  209. PlopDot(w,point)
  210. struct Window *w;
  211. REAL_POINT *point;
  212. {
  213.    DrawImage(w->RPort,&control_image,(SHORT)point->x,(SHORT)point->y);
  214. }
  215.  
  216.    
  217. Draw_ControlPoints(w) 
  218. struct Window *w;
  219. {
  220.    DLISTPTR tmp = &Control_Points;
  221.    while ((tmp = tmp->next) != &Control_Points) 
  222.        PlopDot(w,POINT(tmp));
  223. }
  224.  
  225.  
  226. Redraw(w)
  227. struct Window *w;
  228. {
  229.    struct RastPort *rp = w->RPort;
  230.    SetRast(rp,ERASE);
  231.    SetAPen(rp, CURVECOLOR);
  232.    Draw_ControlPoints(w);
  233.    switch (CurveType) {
  234.      case CLOSEDB :       Draw_Closed_Bspline(w,&Control_Points); break; 
  235.      case CLOSED_INTRPL : Draw_Closed_Ispline(w,&Control_Points); break;
  236.      case OPEN_INTRPL :   Draw_Open_Ispline(w,&Control_Points); break;
  237.      case OPENB_TRIPLE :  Draw_TripleKnot_Bspline(w,&Control_Points); break;
  238.      default: Draw_Natural_Bspline(w,&Control_Points); break;
  239.       }
  240. }
  241.  
  242.  
  243.       
  244.